<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="ruleFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到我的系统</h2>
          <el-form-item prop="name">
            <el-input :prefix-icon="User" v-model="loginForm.name"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login_btn" @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { getTime } from "../../utils/times";
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useCounterStore } from "../../store/index";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const loginForm = reactive({
  name: "admin",
  password: "123456",
});
//加载效果
const loading = ref(false);
const router = useRouter();
const userStore = useCounterStore();
const rules = reactive({
  //规则校验属性
  //required代表这个字段务必校验
  //message校验的提示信息
  //trigger触发校验表单的时机，blur失去焦点，change文本发生变
  name: [
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      required: true,
      min: 3,
      max: 8,
      message: "长度至少3位",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      min: 3,
      max: 15,
      message: "长度至少3位",
      trigger: "change",
    },
  ],
});
const ruleFormRef = ref();

const login = async () => {
  //登录之前触发校验
  await ruleFormRef.value.validate();
  //点击登录时触发加载开始
  loading.value = true;
  try {
    await userStore.userLogin(loginForm); //从store拿到登录数据
    userStore.fetchMenu(loginForm.name); // 登录成功后立即加载菜单
    router.push("/");
    ElMessage({
      type: "success",
      message: `Hi,${getTime()}好，欢迎回来 👋`,
    });
  } catch (error) {
    loading.value = false;
    ElMessage.error(error instanceof Error ? error.message : String(error));
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: url("../../assets/images/background.jpg") no-repeat;
  background-size: cover;
  .login_form {
    text-align: left;
    margin-top: 20vh; // 手动垂直居中
    width: 80%;
    background: url("../../assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3); // 可选，让表单浮起来
    h1 {
      font-size: 40px;
      color: white;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
